﻿using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;

namespace MathTutorPro.Data
{
    public class CustomMigrationsSqlGenerator : MigrationsSqlGenerator
    {
        //public static ISqlManager SqlManager =>
        //SqlManagerHelper.GetSqlManager(AppSettings.Get<DbOptions>()?.GetUseableWriteHost()?.DbType);

        public CustomMigrationsSqlGenerator(
            MigrationsSqlGeneratorDependencies dependencies, 
            IMigrationsAnnotationProvider migrationsAnnotations) : base(dependencies)
        {
        }
        // 禁止生成 CreateTable 中的外键
        protected override void Generate(CreateTableOperation operation, IModel? model, MigrationCommandListBuilder builder, bool terminate = true)
        {
            // 仅对特定表禁用外键
            if (!operation.Name.StartsWith("AspNet")) // 保留 Identity 表的外键
            {
                operation.ForeignKeys.Clear();
            }
            base.Generate(operation, model, builder, terminate);
        }

        // 禁止生成独立的 AddForeignKey 操作
        protected override void Generate(AddForeignKeyOperation operation, IModel? model, MigrationCommandListBuilder builder,
            bool terminate = true)
        {
            if (!operation.Name.StartsWith("AspNet")) // 保留 Identity 表的外键
            {
                // 直接跳过外键生成
                return;
            }
        }

    }
}
